package org.opens.netty.chatServer.message;

import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * Serializable接口使当前类和其子类都具有了序列化的能力
 */
@Data
public abstract class Message implements Serializable {

    public static Class<?> getMessageClass(int messageType) {
        return messageClasses.get(messageType);
    }

    private int sequenceId;

    private int messageType;

    public abstract int getMessageType();

    public static final int loginRequestMessage = 0;
    public static final int loginResponseMessage = 1;

    public static final int chatRequestMessage = 2;
    public static final int chatResponseMessage = 3;

    public static final int groupCreateRequestMessage = 4;
    public static final int groupCreateResponseMessage = 5;

    public static final int groupJoinRequestMessage = 6;
    public static final int groupJoinResponseMessage = 7;

    public static final int groupQuitRequestMessage = 8;
    public static final int groupQuitResponseMessage = 9;

    public static final int groupChatRequestMessage = 10;
    public static final int groupChatResponseMessage = 11;

    public static final int groupMembersRequestMessage = 12;
    public static final int groupMembersResponseMessage = 13;

    public static final int pingMessage = 14;

    public static final int pongMessage = 15;

    public static final int rpcRequestMessage = 101;

    public static final int rpcResponseMessage = 102;

    private static final Map<Integer,Class<?>> messageClasses = new HashMap<>();

    static {
        messageClasses.put(loginRequestMessage, LoginRequestMessage.class);
        messageClasses.put(loginResponseMessage, LoginResponseMessage.class);
        messageClasses.put(chatRequestMessage, ChatRequestMessage.class);
        messageClasses.put(chatResponseMessage, ChatResponseMessage.class);
        messageClasses.put(groupCreateRequestMessage, GroupCreateRequestMessage.class);
        messageClasses.put(groupCreateResponseMessage, GroupCreateResponseMessage.class);
        messageClasses.put(groupJoinRequestMessage, GroupJoinRequestMessage.class);
        messageClasses.put(groupJoinResponseMessage, GroupJoinResponseMessage.class);
        messageClasses.put(groupQuitRequestMessage, GroupQuitRequestMessage.class);
        messageClasses.put(groupQuitResponseMessage, GroupQuitResponseMessage.class);
        messageClasses.put(groupChatRequestMessage, GroupChatRequestMessage.class);
        messageClasses.put(groupChatResponseMessage, GroupChatResponseMessage.class);
        messageClasses.put(groupMembersRequestMessage, GroupMembersRequestMessage.class);
        messageClasses.put(groupMembersResponseMessage, GroupMembersResponseMessage.class);
        messageClasses.put(pingMessage, PingMessage.class);
        messageClasses.put(pongMessage, PongMessage.class);
        messageClasses.put(rpcRequestMessage, RpcRequestMessage.class);
        messageClasses.put(rpcResponseMessage, RpcResponseMessage.class);
    }

}
